﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IYASAKANPCore;
using HelperUnity;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using SendServerLib;
using System.Data;

namespace IYASAKANet_CDDY
{
    public class NetImplementRead : IRead
    {
        // 日志路径.
        private string strLogPath;

        /// <summary>
        /// Sqlite 数据库路径.
        /// </summary>
        private string strDBPath;

        // 是否读取本地车型库.
        private string strIsReadBase;

        public NetImplementRead()
        {
            // 加载配置项.
            this.LoadConfig();
        }

        /// <summary>
        /// 加载配置项.
        /// </summary>
        private void LoadConfig()
        {
            string strPath = NetCommon.GetDllPath() + "//config.ini";
            IniFileOperation iniFile = new IniFileOperation(strPath);

            this.strIsReadBase = iniFile.IniReadValue("VE", "IsReadBase");
            this.strDBPath = iniFile.IniReadValue("DB", "DBPath");

            this.strLogPath = iniFile.IniReadValue("Log", "DllLogPath");
        }

        public string Get19W01(string strVin)
        {
            throw new NotImplementedException();
        }

        public string Get19W02(string strVin)
        {
            throw new NotImplementedException();
        }

        public string Get19W03(string strVin)
        {
            throw new NotImplementedException();
        }

        public string Get19W04(string strVin)
        {
            throw new NotImplementedException();
        }

        public string Get19W05(string strVin)
        {
            throw new NotImplementedException();
        }

        public string Get19W06(string strVin)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据Vin得到车辆信息.
        /// </summary>
        /// <param name="strVin"></param>
        /// <returns></returns>
        public string GetVehiceInfoByVin(string strVin)
        {
            string strResult = string.Empty;
            try
            {
                Dictionary<string, string> dicInfo = new Dictionary<string, string>();
                strResult = NetCommon.GetCorrectResult(dicInfo);
            }

            catch (Exception ex)
            {
                strResult = NetCommon.GetFailResult(ex.Message);
            }

            return strResult;
        }

        /// <summary>
        /// 查询车辆基本信息
        /// </summary>
        /// <param name="strJKID"></param>
        /// <param name="strJsonInfo"></param>
        /// <returns></returns>
        public string QueryObject(string strJKID, string strJsonInfo)
        {
            string strResult = string.Empty;
            try
            {
                NetCommon.WriteLog(this.strLogPath, "R输入信息 " + strJsonInfo);
                JObject json = JObject.Parse(strJsonInfo);

                // 得到Vin
                string strVin = json["vehicleCon"]["vin"].ToString();

                var vConfig = NetCommon.GetAppConfig();
                object[] oArg = new object[] { strVin , "DMS4321" };
                var vRet = WebServiceHelper.InvokeWebService(vConfig[2], vConfig[3], vConfig[4], oArg);

                //vRet.IsSuccess = true;
                //vRet.Data = "{\"rtCode\":\"1\",\"itemVehicle\":[{\"VinCode\":\"LG6ED34H8KY014073\",\"MotorFlag\":\"4B2 - 115C50\",\"CarCode\":\"E3A08Q26B28W5KK42001 - E084P\",\"ColorName\":\"大运环保绿高温漆\",\"MotorCode\":\"Q191102594Z\",\"AfficheType\":\"CGC3041HDD33E\",\"CarWheelbase\":\"2800\",\"zjsj\":\"2019 - 11 - 29 09:42:55\"}]}";
                if (vRet.IsSuccess)
                {
                    JObject jsonResult = JObject.Parse(vRet.Data.ToString());
                    NetCommon.WriteLog(this.strLogPath, "R ws返回信息： " + vRet.Data);

                    if (NetCommon.GetJsonValue(jsonResult["rtCode"]) == "1")
                    {
                        string vehicleModel = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["AfficheType"]);
                        string vin = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["VinCode"]);
                        string xxgkbh = string.Empty;
                        string sb = string.Empty;
                        string sccmc = string.Empty;
                        string sccdz = string.Empty;
                        string hdzkrs = string.Empty;
                        string bsxxs = string.Empty;
                        string qdfs = string.Empty;
                        string qdzw = string.Empty;
                        string cnzzxh = string.Empty;
                        string dcrl = string.Empty;
                        string zbzl = string.Empty;
                        string zzl = string.Empty;
                        string zj = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["CarWheelBase"]);
                        string pqgsl = string.Empty;
                        string pfjd = string.Empty;
                        string fdjsb = string.Empty;
                        string fdjsccdz = string.Empty;
                        string rlzl = string.Empty;
                        string edzs = string.Empty;
                        string fdjpl = string.Empty;
                        string edgl = string.Empty;
                        string fdjqgs = string.Empty;
                        string chzhqk = string.Empty;
                        string zjsj = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["zjsj"]);
                        string fdjh = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["MotorCode"]);
                        string colorname = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["ColorName"]);
                        string scxlh = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["scxlh"]);

                        string strSql = "select * from VehicleRepository where vehiclemodel ='" + vehicleModel + "'";



                        DataTable dt = new DataTable();
                        DbHelper.GetTable(vConfig[1], strSql, ref dt);
                        if (dt.Rows.Count > 0)
                        {
                            sb = dt.Rows[0]["sb"].ToString();
                            sccmc = dt.Rows[0]["sccmc"].ToString();
                            sccdz = dt.Rows[0]["sccdz"].ToString();
                            hdzkrs = dt.Rows[0]["hdzkrs"].ToString();
                            bsxxs = dt.Rows[0]["bsxxs"].ToString();
                            qdfs = dt.Rows[0]["qdfs"].ToString();
                            qdzw = dt.Rows[0]["qdzw"].ToString();
                            cnzzxh = dt.Rows[0]["cnzzxh"].ToString();
                            dcrl = dt.Rows[0]["dcrl"].ToString();
                            zbzl = dt.Rows[0]["zbzl"].ToString();
                            zzl = dt.Rows[0]["zzl"].ToString();
                            if (string.IsNullOrEmpty(zj))
                            {
                                zj = dt.Rows[0]["zj"].ToString();
                            }
                            pqgsl = dt.Rows[0]["pqgsl"].ToString();
                            pfjd = dt.Rows[0]["pfjd"].ToString();
                        }
                        else
                        {
                            NetCommon.WriteLog(this.strLogPath, "R 本地车型库中没有信息： " + vehicleModel);
                        }

                        string fdjbs = NetCommon.GetJsonValue(jsonResult["itemVehicle"][0]["MotorFlag"]);
                        strSql = "select * from FdjRepository where fdjbs='" + fdjbs + "'";

                        DataTable dtFDJ = new DataTable();
                        DbHelper.GetTable(vConfig[1], strSql, ref dtFDJ);

                        if (dtFDJ.Rows.Count > 0)
                        {
                            fdjsb = dtFDJ.Rows[0]["fdjsb"].ToString();
                            fdjsccdz = dtFDJ.Rows[0]["fdjsccdz"].ToString();
                            rlzl = dtFDJ.Rows[0]["rlzl"].ToString();
                            edzs = dtFDJ.Rows[0]["edzs"].ToString();
                            double dPL = 0;
                            bool b = double.TryParse(dtFDJ.Rows[0]["fdjpl"].ToString(), out dPL);
                            if (b)
                            {
                                fdjpl = (dPL / 1000).ToString("f1");
                            }
                            edgl = dtFDJ.Rows[0]["edgl"].ToString();
                            fdjqgs = dtFDJ.Rows[0]["fdjqgs"].ToString();
                            chzhqk = dtFDJ.Rows[0]["chzhqk"].ToString();
                        }
                        else
                        {
                            NetCommon.WriteLog(this.strLogPath, "R 本地车型库中没有信息： " + vehicleModel);
                        }

                        Dictionary<string, string> dicInfo = new Dictionary<string, string>();
                        dicInfo.Add("vehicleModel", vehicleModel);
                        dicInfo.Add("vin", vin);
                        dicInfo.Add("xxgkbh", xxgkbh);
                        dicInfo.Add("sb", sb);
                        dicInfo.Add("sccmc", sccmc);
                        dicInfo.Add("sccdz", sccdz);
                        dicInfo.Add("scdate", DateTime.Now.ToString("yyyyMMdd"));
                        dicInfo.Add("fdjh", fdjh);
                        dicInfo.Add("fdjsb", fdjsb);
                        dicInfo.Add("fdjsccdz", fdjsccdz);
                        dicInfo.Add("rlzl", rlzl);
                        dicInfo.Add("hdzkrs", hdzkrs);
                        dicInfo.Add("bsxxs", bsxxs);
                        dicInfo.Add("qdfs", qdfs);
                        dicInfo.Add("qdzw", qdzw);
                        dicInfo.Add("cnzzxh", cnzzxh);
                        dicInfo.Add("dcrl", dcrl);
                        dicInfo.Add("fdjpl", fdjpl);
                        dicInfo.Add("edgl", edgl);
                        dicInfo.Add("chzhqqk", chzhqk);
                        dicInfo.Add("fdjqgs", fdjqgs);
                        dicInfo.Add("zbzl", zbzl);
                        dicInfo.Add("zzl", zzl);
                        dicInfo.Add("zj", zj);
                        dicInfo.Add("edzs", edzs);
                        dicInfo.Add("pqgsl", pqgsl);
                        dicInfo.Add("zjsj", zjsj);
                        dicInfo.Add("colorname", colorname);
                        dicInfo.Add("scxlh", scxlh);

                        strResult = NetCommon.GetCorrectResult(dicInfo);

                        NetCommon.WriteLog(this.strLogPath, "R 返回结果： " + strResult);
                    }
                    else
                    {
                        strResult = NetCommon.GetFailResult("u9系统中未查出此VIN");
                    }
                }
                else
                {
                    NetCommon.WriteLog(this.strLogPath, "R访问出错： " + vRet.Message);
                }

            }
            catch (Exception ex)
            {
                strResult = NetCommon.GetFailResult(ex.Message);
            }

            NetCommon.WriteLog(this.strLogPath, "R输出信息 " + strResult);

            return strResult;
        }
    }
}
